package com.xiangban.volunteer_platform.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangban.volunteer_platform.dao.UserMapper;
import com.xiangban.volunteer_platform.entity.Ids;
import com.xiangban.volunteer_platform.entity.User;
import com.xiangban.volunteer_platform.entity.dto.LeaderIdDto;
import com.xiangban.volunteer_platform.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/1/27 12:42
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public int save(User user) {
        user.setUserId(RandomUtil.getUUID());

        Date date = new Date();
        user.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
        user.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
        user.setStatus(1);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 != null){
            if (user1.getUsername().equals(user.getUsername())){
                return -1;
            }
        }
        int insert = userMapper.insert(user);
        return insert;
    }


    public int update(User user) {
        Date date = new Date();
        user.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserId,user.getUserId());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 != null){
//            if (user1.getUserId().equals(user.getUserId())){
                return  userMapper.updateById(user);
//            }
        }
          return -1;
    }

    public void del(User user) {
        userMapper.deleteById(user.getUserId());
    }

    public IPage<User> getAll(Integer pageNum, Integer pageSize, User user) {
        IPage<User> page = new Page<>(pageNum == null ? 1 : pageNum,pageSize == null ? 10 : pageSize);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

        if (user.getNickname() != null && !user.getNickname().equals("")){
            queryWrapper.like(User::getNickname,user.getNickname());
        }
        if (user.getMobile() != null && !user.getMobile().equals("")){
            queryWrapper.eq(User::getMobile,user.getMobile());
        }
        if (user.getType() != null && !user.getType().equals(0)){
            queryWrapper.eq(User::getType,user.getType());
        }
        if (user.getCopId() != null && !user.getCopId().equals(0)){
            queryWrapper.eq(User::getCopId,user.getCopId());
        }
       return userMapper.selectPage(page,queryWrapper);
    }

    public User getUser(User user) {
       LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.eq(User::getOpenId,user.getOpenId());
        return userMapper.selectOne(queryWrapper);
    }

    public User getUserById(User user){

       return userMapper.selectById(user.getUserId());
    }
    
    public Object byBhone(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getMobile,user.getMobile());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 != null){
            return user1.getUserId();
        }
        return -1;
    }

    public List<User> getList() {

        return userMapper.getUserByType();
    }

    public List<User> getListById(Ids ids){
      return   userMapper.selectBatchIds(ids.getIds());
    }


    public int resetting(LeaderIdDto leaderIdDto){
        if (leaderIdDto.getLeaderId() != null && !leaderIdDto.getLeaderId().equals("")){
           return userMapper.resetting(leaderIdDto.getLeaderId());
        }
        return -1;
    }

    public int updatePersonListsByLeaderIdList(LeaderIdDto leaderIdDto){
        if (leaderIdDto != null && !leaderIdDto.equals("")){
            return userMapper.updatePersonListsByLeaderIdList(leaderIdDto);
        }
        return -1;
    }

}
